<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 * Simple utility class to handle logins.
 */
class Login_Manager {

    var $logged_in_user = NULL;

    function __construct($params = array()) {
        $this->CI = & get_instance();
        $this->session = & $this->CI->session;

        if (!isset($params['autologin']) || $params['autologin'] !== FALSE) {
            $required_group = -1;
            if (isset($params['required_group'])) {
                $required_group = $params['required_group'];
            }
            $this->check_login($required_group);
        }
    }

    function check_login($required_group = -1) {
        // if not logged in, automatically redirect
        $user = $this->get_user();
        if ($user === FALSE) {
            $this->session->set_userdata('login_redirect', uri_string());
            redirect('auth/cpanel/login');
        }
        if ($required_group > 0) {
            if ($user->group->id > $required_group) {
                show_error('You do not have access to this section.');
            }
        }
    }

    /**
     * process_login
     * Validates that a username and password are correct.
     * 
     * @param object $userser The user containing the login information.
     * @return FALSE if invalid, TRUE or a redirect string if valid. 
     */
    function process_login($userser) {
        // attempt the login
        $success = $userser->login();//print_r($success);exit;
        if ($success) {
            // store the userid if the login was successful
            $this->session->set_userdata('logged_in_id', $userser->id);
            // store the user for this request
            $this->logged_in_user = $userser;
            // if a redirect is necessary, return it.
            $redirect = $this->session->userdata('login_redirect');//print_r($redirect);exit;
            if (!empty($redirect)) {
                $success = $redirect;
            }
        }
        return $success;
    }

    function logout() {
        session_destroy();
        $this->session->sess_destroy();
        $this->logged_in_user = NULL;
    }

    function get_user() {
        if (is_null($this->logged_in_user)) {
            $id = $this->session->userdata('logged_in_id');
            if (is_numeric($id)) {
                $user = new User_model();
                $user->select('id, firstname, lastname, username');
                $user->get_by_id($id);
                if ($user->exists()) {
                    $user->group->get(1);
                    $this->logged_in_user = $user;

                    return $this->logged_in_user;
                }
            }
            return FALSE;
        } else {
            return $this->logged_in_user;
        }
    }

    public function check_role($code) {
        $user = new User_model();
        $user = $this->get_user();
        $group = new Group_model();
        $group->where_related_user('id', $user->id)->where_related_role('code', $code)->get_iterated();
        $result = $group->result_count();
        if ($result == 0) {
            $error_message = 'Bạn không có quyền truy cập chức năng này';
            $this->session->set_flashdata('error_message',$error_message);
            redirect('auth/cpanel/index');
        }
    }

}
